package com.yz.jdd.base.activity;


import android.app.Dialog;
import android.content.Context;
import android.graphics.drawable.Animatable;
import android.graphics.drawable.AnimationDrawable;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.KeyEvent;
import android.view.View;
import android.view.WindowManager.LayoutParams;
import android.widget.ImageView;
import android.widget.TextView;


import com.yz.jdd.R;

import rx.Subscription;

public class LoadingDialog extends Dialog {

    private String msg;
    Subscription subscription;
    private Boolean isCancelable=true;
    private ImageView iv;
    private AnimationDrawable mAnimation;

    public LoadingDialog(Context context, String msg) {
        super(context, R.style.Cate_Dialog);
//        super(context, android.support.v7.appcompat.R.style.Base_Theme_AppCompat_Dialog);
        setContentView(R.layout.loading_dialog);
        this.msg = msg;
        if (!TextUtils.isEmpty(msg)) {
            TextView msgText = (TextView) findViewById(R.id.loading_msg);
            msgText.setText(msg);
            msgText.setVisibility(View.VISIBLE);
        }
//        ImageView iv=findViewById(R.id.loading_progressbar);
//        ((Animatable)iv.getDrawable()).start();


//        initLayoutWindow();
    }

    private void initLayoutWindow() {
        LayoutParams p = getWindow().getAttributes();
        p.gravity = Gravity.CENTER;
        p.height = LayoutParams.WRAP_CONTENT;//(int) (d.getWidth() * 0.5);   //高度设置为屏幕的0.4
        p.width = LayoutParams.WRAP_CONTENT;//(int) (d.getWidth() * 0.85);    //宽度设置为屏幕的0.7
        getWindow().setAttributes(p);
    }

    public void setGroundNoBlack() {
        LayoutParams p = getWindow().getAttributes();
        p.flags &= ~LayoutParams.FLAG_DIM_BEHIND;
        p.gravity = Gravity.CENTER;
        getWindow().setAttributes(p);
    }


    public void setCancelable(boolean cancelable)
    {
        isCancelable=cancelable;
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if(keyCode== KeyEvent.KEYCODE_BACK)
        {
            if(!isCancelable)
                return true;
            this.dismiss();
        }
        return super.onKeyDown(keyCode, event);
    }

    @Override
    public void dismiss() {
        super.dismiss();
        if (subscription != null)
            subscription.unsubscribe();
    }
}
